package cn.thens.kdroid.sample.common.app

import android.app.Activity
import android.view.View
import com.google.android.material.snackbar.Snackbar

interface Snacker {
    val snackContainer: View

    fun makeSnack(text: CharSequence, duration: Int = Snackbar.LENGTH_SHORT): Snackbar {
        return Snackbar.make(snackContainer, text, duration)
    }

    fun showSnack(text: CharSequence, duration: Int = Snackbar.LENGTH_SHORT) {
        makeSnack(text, duration).show()
    }

    companion object {
        fun create(view: View): Snacker {
            return object : Snacker {
                override val snackContainer: View = view
            }
        }
    }
}

fun Activity.makeSnack(text: CharSequence, duration: Int = Snackbar.LENGTH_SHORT): Snackbar {
    return Snacker.create(findViewById(android.R.id.content)).makeSnack(text, duration)
}

fun Activity.showSnack(text: CharSequence, duration: Int = Snackbar.LENGTH_SHORT) {
    makeSnack(text, duration).show()
}